/**
 * Copyright (c) 2021 Coder League
 * All rights reserved.
 *
 * File：GoodDaoImpl.java
 * History:
 *         2021年1月5日: Initially created, Chrise.
 */
package club.coderleague.cfss.dao.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import club.coderleague.cfss.dao.extension.GoodDaoExtension;
import club.coderleague.cfss.domain.beans.GoodSearcher;
import club.coderleague.cfss.domain.beans.GoodUpdateRequest;
import club.coderleague.cfss.domain.beans.ListGood;
import club.coderleague.cfss.domain.beans.OrderGoodCheck;
import club.coderleague.cfss.domain.beans.ViewGoodDetail;
import club.coderleague.cfss.domain.beans.ViewListGood;
import club.coderleague.cfss.domain.entities.Goods;
import club.coderleague.cfss.domain.enums.EntityState;
import club.coderleague.cfss.domain.enums.PictureType;
import club.coderleague.cfss.util.CommonUtil;
import club.coderleague.data.jpa.id.support.SnowflakeV4;
import club.coderleague.data.jpa.repository.support.AbstractDataRepositoryExtension;

/**
 * 商品数据访问对象实现。
 * @author Chrise
 */
public class GoodDaoImpl extends AbstractDataRepositoryExtension<Goods, Long, SnowflakeV4> implements GoodDaoExtension {
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#queryMgrListData(long, java.lang.String)
	 */
	@Override
	public List<ListGood> queryMgrListData(long supplier, String goodpic) {
		String sql = "select g.entityid as goodid, g.goodcode, g.goodname, g.goodspecs, g.goodprice, g.meteringunit, g.manufacturer, " 
			+ "(select concat('" + goodpic + "', gp.picturepath) from Goodpictures gp " 
				+ "where gp.goodid = g.entityid and gp.entitystate = :gpstate and gp.picturetype = :gptype) as goodpic " 
			+ "from Goods g where g.supplierid = :supplier and g.entitystate in (:gstate) order by g.entityid desc";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("supplier", supplier);
		params.put("gstate", Arrays.asList(EntityState.VALID.getValue(), EntityState.ONSALE.getValue()));
		params.put("gpstate", EntityState.VALID.getValue());
		params.put("gptype", PictureType.COVER.getValue());
		
		return super.queryCustomBeanListBySql(ListGood.class, sql, params);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#queryMgrGoodData(long)
	 */
	@Override
	public GoodUpdateRequest queryMgrGoodData(long goodid) {
		String sql = "select goodname, goodcode, goodspecs, meteringunit, goodprice, manufacturer, gooddetail from Goods where entityid = ?0";
		return super.queryCustomBeanBySql(GoodUpdateRequest.class, sql, goodid);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#existsGood(long, java.lang.String, java.lang.String, java.lang.Long)
	 */
	@Override
	public boolean existsGood(long supplier, String name, String code, Long id) {
		String sql = "select count(entityid) from goods where entitystate in (:entitystate) " 
			+ "and supplierid = :supplierid and (goodname = :goodname or goodcode = :goodcode)";
		
		Map<String, Object> params = new HashMap<>();
		params.put("entitystate", Arrays.asList(EntityState.VALID.getValue(), EntityState.ONSALE.getValue()));
		params.put("supplierid", supplier);
		params.put("goodname", name);
		params.put("goodcode", code);
		
		if (id != null) {
			sql += " and entityid != :entityid";
			params.put("entityid", id);
		}
		
		return (super.queryLongBySql(sql, params) > 0);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#queryViewListData(club.coderleague.cfss.domain.beans.GoodSearcher, java.lang.String)
	 */
	@Override
	public List<ViewListGood> queryViewListData(GoodSearcher searcher, String goodpic) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("gstate", EntityState.ONSALE.getValue());
		params.put("provinceid", searcher.getProvinceid());
		params.put("gpstate", EntityState.VALID.getValue());
		params.put("gptype", PictureType.COVER.getValue());
		
		String keyword = "", area = "";
		if (CommonUtil.isNotEmpty(searcher.getKeyword())) {
			keyword += "and (g.goodname like :keyword or g.supplierid in (select s.entityid from Suppliers s where s.entname like :keyword and s.entitystate = :sstate)) ";
			params.put("keyword", "%" + searcher.getKeyword() + "%");
			params.put("sstate", EntityState.OPENING.getValue());
		}
		if (searcher.getCityid() != null) {
			area += " and da.cityid = :cityid";
			params.put("cityid", searcher.getCityid());
		}
		if (searcher.getDistrictid() != null) {
			area += " and da.districtid = :districtid";
			params.put("districtid", searcher.getDistrictid());
		}
		if (searcher.getStreetid() != null) {
			area += " and da.streetid = :streetid";
			params.put("streetid", searcher.getStreetid());
		}
		
		String sql = "select g.entityid as goodid, g.goodcode, g.goodname, g.goodspecs, g.goodprice, g.meteringunit, g.manufacturer, " 
			+ "(select concat('" + goodpic + "', gp.picturepath) from Goodpictures gp " 
				+ "where gp.goodid = g.entityid and gp.entitystate = :gpstate and gp.picturetype = :gptype) as goodpic, g.supplierid " 
			+ "from Goods g where g.entitystate = :gstate " + keyword 
				+ "and g.supplierid in (select da.supplierid from Distributionareas da where da.provinceid = :provinceid" + area + ") " 
			+ "order by g.supplierid, g.entityid desc";
		
		return super.queryCustomBeanListBySql(ViewListGood.class, sql, params);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#checkGoods(java.util.List)
	 */
	@Override
	public List<ListGood> checkGoods(List<Long> goodids) {
		String sql = "select g.entityid as goodid, g.goodname, g.goodprice " 
			+ "from Goods g where g.entitystate = :goodstate and g.entityid in (:goodid)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodstate", EntityState.ONSALE.getValue());
		params.put("goodid", goodids);
		return super.queryCustomBeanListBySql(ListGood.class, sql, params);
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#queryViewDetailData(long)
	 */
	@Override
	public ViewGoodDetail queryViewDetailData(long goodid) {
		String sql = "select goodcode, goodname, goodspecs, goodprice, meteringunit, manufacturer, gooddetail " 
			+ "from Goods where entityid = ?0 and entitystate = ?1";
		return super.queryCustomBeanBySql(ViewGoodDetail.class, sql, goodid, EntityState.ONSALE.getValue());
	}
	
	/**
	 * @see club.coderleague.cfss.dao.extension.GoodDaoExtension#checkOrderGoods(java.util.List)
	 */
	@Override
	public List<OrderGoodCheck> checkOrderGoods(List<Long> goodids) {
		String sql = "select g.entityid as goodid, g.supplierid, g.goodname, g.goodspecs, g.meteringunit, g.goodprice " 
			+ "from Goods g where g.entitystate = :goodstate and g.entityid in (:goodid) order by g.supplierid";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodstate", EntityState.ONSALE.getValue());
		params.put("goodid", goodids);
		return super.queryCustomBeanListBySql(OrderGoodCheck.class, sql, params);
	}
}
